package com.fingard.dsp.bank.directbank.bea01;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.WebRequest;
import com.fingard.text.StringHelper;
import com.fingard.util.TransIDHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class BEA01Base extends DirectBase {
    private static TransIDHelper idHelper = new TransIDHelper(1, 999);

    protected static boolean needChangePwd = true;

    protected String getQryTransID() {
        Date tmpDateNow = new Date();
        return Format.DateTime17Format.format(tmpDateNow) + idHelper.getNextIDStr();
    }

    protected String getCharset() {
        return getBankConfig().getCharset("UTF-8");
    }

    protected void writeCommonHeader(XmlTextWriter xmlWriter, BEA01Header p_bankHeader) throws Exception {
        if (StringHelper.isNullOrEmpty(p_bankHeader.SERVICE_CODE)) {
            throw new Exception("未赋值交易代码");
        }
        if (StringHelper.isNullOrEmpty(p_bankHeader.TRAN_REF_NO)) {
            throw new Exception("未赋值指令序列号");
        }

        xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"" + getCharset() + "\"?>");
        xmlWriter.writeStartElement("bcdl");
        xmlWriter.writeElementString("SERVICE_CODE", p_bankHeader.SERVICE_CODE);
        xmlWriter.writeElementString("TRAN_REF_NO", p_bankHeader.TRAN_REF_NO);
    }

    protected String[] sendToBank(String p_reqStr) throws Exception {
        ifChangePwdSucc();
        if (needChangePwd) {
            return new String[]{"修改密码失败！", ""};
        }

        String[] retStr = new String[]{"", ""};
        String tmpUrl = "http://" + bankFront.serverIP + ":" + bankFront.serverPort + "/bcdlserver/bcdlserverServlet";
        WriteBankLogLn("2.提交银行的地址：" + tmpUrl);
        WriteBankLogStep2(p_reqStr);
        WebRequest tmpWebRequest = new WebRequest(tmpUrl);
        tmpWebRequest.setConnectTimeout(100000);
        tmpWebRequest.setRequestProperty("accept", "application/xml");
        tmpWebRequest.setRequestProperty("Content-Type", "applocation/xml;charset=utf-8");
        byte[] tmpUpBytes = p_reqStr.getBytes(getCharset());
        retStr = tmpWebRequest.upload(tmpUpBytes, getCharset());
        WriteBankLogStep3(retStr[1]);
        retStr[1] = retStr[1].replace("<RETURN_MESSAGE>", "").replace("</RETURN_MESSAGE>", "");
        return retStr;
    }

    protected String[] sendToBankForPwdChange(String p_reqStr) throws Exception {
        String[] retStr = new String[]{"", ""};
        String tmpUrl = "http://" + bankFront.serverIP + ":" + bankFront.serverPort + "/bcdlserver/bcdlserverServlet";
        WriteBankLogLn("2.提交银行的地址：" + tmpUrl);
        WriteBankLogStep2(p_reqStr);
        WebRequest tmpWebRequest = new WebRequest(tmpUrl);
        tmpWebRequest.setConnectTimeout(100000);
        tmpWebRequest.setRequestProperty("accept", "application/xml");
        tmpWebRequest.setRequestProperty("Content-Type", "applocation/xml;charset=utf-8");
        byte[] tmpUpBytes = p_reqStr.getBytes(getCharset());
        retStr = tmpWebRequest.upload(tmpUpBytes, getCharset());
        WriteBankLogStep3(retStr[1]);
        retStr[1] = retStr[1].replace("<RETURN_MESSAGE>", "").replace("</RETURN_MESSAGE>", "");
        return retStr;
    }

    private void ifChangePwdSucc() throws Exception {
        if (getBankConfig().getValueAsBool("autoChangePwd") && needChangePwd) {
            BEA01Header tmpHeader = new BEA01Header();
            tmpHeader.SERVICE_CODE = "1101";
            tmpHeader.TRAN_REF_NO = getQryTransID();

            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, tmpHeader);
            xmlWriter.writeElementString("reqTime", "", true);
            xmlWriter.writeElementString("OIDPASSWORD", "123456");
            xmlWriter.writeElementString("NEWPASSWORD", getBankConfig().getValueAsString("newPwd"));
            xmlWriter.writeEndDocument();

            String[] retStrArr = sendToBankForPwdChange(xmlWriter.toXmlString());
            if (retStrArr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(retStrArr[1], tmpSbMsg);

                if (tmpBkReader != null) {
                    String retCode = tmpBkReader.getFirstText("/BCDL/opRep/retCode");
                    String errMsg = tmpBkReader.getFirstText("/BCDL/opRep/errMsg");
                    if ("000000".equals(retCode) || "API111116".equals(retCode)) {
                        needChangePwd = false;
                    }
                }
            }
        } else {
            needChangePwd = false;
        }

    }
}
